module.exports = {
    async createDataBase(instance, dbName) {
        let OperateStatus;
        let FinalValue;
        let createOperatePromise = new Promise((resolve, reject) => {
            let db_create = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            db_create.connect((error, result) => {
                if (error === null || error === undefined) {
                    let confirmName = result.db("tg").admin();
                    confirmName.buildInfo((err) => {
                        if (err === null || err === undefined) {
                            resolve(`创建数据库“${dbName}”成功。`);
                        } else {
                            reject(`创建数据库“${dbName}”失败。详情：${err}`);
                        }
                        db_create.close(true);
                    })
                } else {
                    reject(error);
                    db_create.close(true);
                }
            });
        });
        await createOperatePromise.then((val) => {
            OperateStatus = true;
            FinalValue = val;
        }).catch((val) => {
            OperateStatus = false;
            FinalValue = val;
        });
        if (OperateStatus) {
            return FinalValue;
        } else {
            throw new Error(FinalValue);
        }
    },
    async removeDataBase(instance, dbName) {
        let OperateStatus;
        let FinalValue;
        let removeOperatePromise = new Promise((resolve, reject) => {
            let db_remove = new instance.__InstanceRootControl.PluginPoint.mongodb.MongoClient(
                instance.__libs.Tools.__SpecialTools.getUrl(instance.__InstanceRootControl.confirmFinalData),
                {useUnifiedTopology: true}
            );
            db_remove.connect((error, result) => {
                if (error === null || error === undefined) {
                    result.db(dbName).dropDatabase((err, res) => {
                        if (err === null || err === undefined) {
                            resolve(`删除数据库“${dbName}”成功。`);
                        } else {
                            reject(`删除数据库“${dbName}”失败。详情：${err}`);
                        }
                        db_remove.close(true);
                    });
                } else {
                    reject(error);
                    db_remove.close(true);
                }
            });
        });
        await removeOperatePromise.then((val) => {
            OperateStatus = true;
            FinalValue = val;
        }).catch((val) => {
            OperateStatus = false;
            FinalValue = val;
        });
        if (OperateStatus) {
            return FinalValue;
        } else {
            throw new Error(FinalValue);
        }
    }
}